import type {
  HistoricalEvent,
  Kingdom,
  ZhuMember,
} from '../../types/zhu-family';

/**
 * 朱氏皇族成员数据
 */
export const zhuMembers: ZhuMember[] = [
  // 第一代 - 洪武帝
  {
    id: 'zhu-1',
    name: '朱元璋',
    title: '太祖洪武皇帝',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=ZhuYuanzhang',
    generation: 1,
    gender: 'male',
    birthDate: '1328-10-21',
    deathDate: '1398-06-24',
    reignPeriod: '1368-1398',
    role: 'emperor',
    kingdomId: 'central',
    hasSBT: true,
    sbtTokenId: 1001,
    achievements: [
      '建立明朝',
      '驱除鞑虏，恢复中华',
      '制定《大明律》',
      '设立分封制度',
    ],
    children: [
      'zhu-2-1',
      'zhu-2-2',
      'zhu-2-3',
      'zhu-2-4',
      'zhu-2-5',
      'zhu-2-6',
      'zhu-2-7',
      'zhu-2-8',
    ],
  },

  // 第二代 - 藩王们
  {
    id: 'zhu-2-1',
    name: '朱标',
    title: '太子懿文',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=ZhuBiao',
    generation: 2,
    gender: 'male',
    birthDate: '1355-10-10',
    deathDate: '1392-05-17',
    role: 'crown-prince',
    kingdomId: 'central',
    hasSBT: true,
    sbtTokenId: 1002,
    achievements: ['储君'],
    parentId: 'zhu-1',
    children: ['zhu-3-1'],
  },
  {
    id: 'zhu-2-2',
    name: '朱樉',
    title: '秦愍王',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=ZhuShuang',
    generation: 2,
    gender: 'male',
    birthDate: '1356-12-03',
    deathDate: '1395-04-09',
    role: 'prince',
    kingdomId: 'qin',
    fiefdom: '西安',
    hasSBT: true,
    sbtTokenId: 1003,
    achievements: ['镇守西北', '秦藩创始人'],
    parentId: 'zhu-1',
  },
  {
    id: 'zhu-2-3',
    name: '朱棡',
    title: '晋恭王',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=ZhuGang',
    generation: 2,
    gender: 'male',
    birthDate: '1358-03-15',
    deathDate: '1398-03-09',
    role: 'prince',
    kingdomId: 'jin',
    fiefdom: '太原',
    hasSBT: true,
    sbtTokenId: 1004,
    achievements: ['晋藩创始人', '屏障北疆'],
    parentId: 'zhu-1',
  },
  {
    id: 'zhu-2-4',
    name: '朱棣',
    title: '成祖永乐皇帝',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=ZhuDi',
    generation: 2,
    gender: 'male',
    birthDate: '1360-05-02',
    deathDate: '1424-08-12',
    reignPeriod: '1402-1424',
    role: 'emperor',
    kingdomId: 'yan',
    fiefdom: '北京',
    hasSBT: true,
    sbtTokenId: 1005,
    achievements: [
      '靖难之役',
      '迁都北京',
      '郑和下西洋',
      '永乐大典',
      '五征漠北',
    ],
    parentId: 'zhu-1',
    children: ['zhu-3-2', 'zhu-3-3'],
  },
  {
    id: 'zhu-2-5',
    name: '朱橚',
    title: '周定王',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=ZhuSu',
    generation: 2,
    gender: 'male',
    birthDate: '1361-08-09',
    deathDate: '1425-06-12',
    role: 'prince',
    kingdomId: 'zhou',
    fiefdom: '开封',
    hasSBT: true,
    sbtTokenId: 1006,
    achievements: ['周藩创始人', '编撰《救荒本草》'],
    parentId: 'zhu-1',
  },
  {
    id: 'zhu-2-6',
    name: '朱桢',
    title: '楚昭王',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=ZhuZhen',
    generation: 2,
    gender: 'male',
    birthDate: '1364-01-03',
    deathDate: '1424-10-06',
    role: 'prince',
    kingdomId: 'chu',
    fiefdom: '武昌',
    hasSBT: true,
    sbtTokenId: 1007,
    achievements: ['楚藩创始人', '镇守荆湖'],
    parentId: 'zhu-1',
  },
  {
    id: 'zhu-2-7',
    name: '朱椿',
    title: '蜀献王',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=ZhuChun',
    generation: 2,
    gender: 'male',
    birthDate: '1371-02-16',
    deathDate: '1423-07-22',
    role: 'prince',
    kingdomId: 'shu',
    fiefdom: '成都',
    hasSBT: true,
    sbtTokenId: 1008,
    achievements: ['蜀藩创始人', '《蜀府集》编纂'],
    parentId: 'zhu-1',
  },
  {
    id: 'zhu-2-8',
    name: '朱柏',
    title: '湘献王',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=ZhuBai',
    generation: 2,
    gender: 'male',
    birthDate: '1371-09-18',
    deathDate: '1399-10-15',
    role: 'prince',
    kingdomId: 'xiang',
    fiefdom: '长沙',
    hasSBT: true,
    sbtTokenId: 1009,
    achievements: ['湘藩创始人'],
    parentId: 'zhu-1',
  },

  // 第三代
  {
    id: 'zhu-3-1',
    name: '朱允炆',
    title: '建文帝',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=ZhuYunwen',
    generation: 3,
    gender: 'male',
    birthDate: '1377-12-05',
    deathDate: '1402-07-13',
    reignPeriod: '1398-1402',
    role: 'emperor',
    kingdomId: 'central',
    hasSBT: true,
    sbtTokenId: 1010,
    achievements: ['削藩政策', '文治复兴'],
    parentId: 'zhu-2-1',
  },
  {
    id: 'zhu-3-2',
    name: '朱高炽',
    title: '仁宗洪熙皇帝',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=ZhuGaochi',
    generation: 3,
    gender: 'male',
    birthDate: '1378-08-16',
    deathDate: '1425-05-29',
    reignPeriod: '1424-1425',
    role: 'emperor',
    kingdomId: 'central',
    hasSBT: true,
    sbtTokenId: 1011,
    achievements: ['仁政宽厚', '罢西洋'],
    parentId: 'zhu-2-4',
    children: ['zhu-4-1'],
  },
  {
    id: 'zhu-3-3',
    name: '朱高煦',
    title: '汉王',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=ZhuGaoxu',
    generation: 3,
    gender: 'male',
    birthDate: '1380-04-14',
    deathDate: '1426-09-22',
    role: 'prince',
    kingdomId: 'han',
    fiefdom: '青州',
    hasSBT: true,
    sbtTokenId: 1012,
    achievements: ['靖难功臣'],
    parentId: 'zhu-2-4',
  },

  // 第四代
  {
    id: 'zhu-4-1',
    name: '朱瞻基',
    title: '宣宗宣德皇帝',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=ZhuZhanji',
    generation: 4,
    gender: 'male',
    birthDate: '1398-03-16',
    deathDate: '1435-01-31',
    reignPeriod: '1425-1435',
    role: 'emperor',
    kingdomId: 'central',
    hasSBT: true,
    sbtTokenId: 1013,
    achievements: ['宣德盛世', '促成仁宣之治', '书画造诣'],
    parentId: 'zhu-3-2',
  },
];

/**
 * 藩国/王国数据
 */
export const kingdoms: Kingdom[] = [
  {
    id: 'central',
    name: '中央朝廷',
    capital: '南京/北京',
    location: { lat: 39.9042, lng: 116.4074 },
    type: 'imperial',
    establishedYear: 1368,
    founder: '朱元璋',
    currentRuler: '朱瞻基',
    treasury: 50_000_000,
    population: 60_000_000,
    status: 'active',
    description: '大明帝国中央政权，统领天下',
  },
  {
    id: 'qin',
    name: '秦藩',
    capital: '西安',
    location: { lat: 34.2658, lng: 108.9541 },
    type: 'princedom',
    establishedYear: 1370,
    founder: '朱樉',
    currentRuler: '朱樉',
    treasury: 3_000_000,
    population: 5_000_000,
    status: 'active',
    description: '镇守西北，屏障关中',
  },
  {
    id: 'jin',
    name: '晋藩',
    capital: '太原',
    location: { lat: 37.8706, lng: 112.5489 },
    type: 'princedom',
    establishedYear: 1370,
    founder: '朱棡',
    currentRuler: '朱棡',
    treasury: 2_500_000,
    population: 4_000_000,
    status: 'active',
    description: '晋阳重地，屏障北疆',
  },
  {
    id: 'yan',
    name: '燕藩（后为帝都）',
    capital: '北京',
    location: { lat: 39.9042, lng: 116.4074 },
    type: 'princedom',
    establishedYear: 1370,
    founder: '朱棣',
    currentRuler: '朱棣',
    treasury: 8_000_000,
    population: 8_000_000,
    status: 'upgraded',
    description: '靖难后升格为帝都',
  },
  {
    id: 'zhou',
    name: '周藩',
    capital: '开封',
    location: { lat: 34.7971, lng: 113.6434 },
    type: 'princedom',
    establishedYear: 1370,
    founder: '朱橚',
    currentRuler: '朱橚',
    treasury: 3_500_000,
    population: 6_000_000,
    status: 'active',
    description: '汴梁重镇，文化中心',
  },
  {
    id: 'chu',
    name: '楚藩',
    capital: '武昌',
    location: { lat: 30.5928, lng: 114.3055 },
    type: 'princedom',
    establishedYear: 1370,
    founder: '朱桢',
    currentRuler: '朱桢',
    treasury: 4_000_000,
    population: 7_000_000,
    status: 'active',
    description: '荆楚之地，水运枢纽',
  },
  {
    id: 'shu',
    name: '蜀藩',
    capital: '成都',
    location: { lat: 30.5728, lng: 104.0668 },
    type: 'princedom',
    establishedYear: 1371,
    founder: '朱椿',
    currentRuler: '朱椿',
    treasury: 3_000_000,
    population: 5_500_000,
    status: 'active',
    description: '天府之国，富庶安宁',
  },
  {
    id: 'xiang',
    name: '湘藩',
    capital: '长沙',
    location: { lat: 28.2282, lng: 112.9388 },
    type: 'princedom',
    establishedYear: 1370,
    founder: '朱柏',
    currentRuler: '朱柏',
    treasury: 2_000_000,
    population: 4_000_000,
    status: 'active',
    description: '湘江之滨，鱼米之乡',
  },
  {
    id: 'han',
    name: '汉藩',
    capital: '青州',
    location: { lat: 36.6847, lng: 118.4797 },
    type: 'princedom',
    establishedYear: 1403,
    founder: '朱高煦',
    currentRuler: '朱高煦',
    treasury: 1_500_000,
    population: 3_000_000,
    status: 'active',
    description: '齐鲁之地，靖难封赏',
  },
];

/**
 * 历史大事件
 */
export const historicalEvents: HistoricalEvent[] = [
  {
    id: 'event-1',
    title: '明朝建立',
    date: '1368-01-23',
    type: 'political',
    importance: 'critical',
    description: '朱元璋在应天府称帝，国号大明，年号洪武',
    participants: ['zhu-1'],
    location: '南京',
    onChainHash: '0x1234...abcd',
  },
  {
    id: 'event-2',
    title: '分封诸王',
    date: '1370-04-13',
    type: 'governance',
    importance: 'critical',
    description: '洪武帝分封诸子为藩王，建立藩国制度',
    participants: [
      'zhu-1',
      'zhu-2-2',
      'zhu-2-3',
      'zhu-2-4',
      'zhu-2-5',
      'zhu-2-6',
      'zhu-2-7',
    ],
    location: '南京',
    onChainHash: '0x2345...bcde',
  },
  {
    id: 'event-3',
    title: '靖难之役',
    date: '1399-08-06',
    type: 'military',
    importance: 'critical',
    description: '燕王朱棣起兵反建文帝，史称靖难之役',
    participants: ['zhu-2-4', 'zhu-3-1'],
    location: '北京-南京',
    onChainHash: '0x3456...cdef',
  },
  {
    id: 'event-4',
    title: '永乐迁都',
    date: '1421-01-01',
    type: 'political',
    importance: 'major',
    description: '永乐帝迁都北京，加强北方防御',
    participants: ['zhu-2-4'],
    location: '北京',
    onChainHash: '0x4567...def0',
  },
  {
    id: 'event-5',
    title: '郑和下西洋',
    date: '1405-07-11',
    type: 'cultural',
    importance: 'major',
    description: '郑和率船队首次下西洋，展示国威',
    participants: ['zhu-2-4'],
    location: '南京-海外',
    onChainHash: '0x5678...ef01',
  },
  {
    id: 'event-6',
    title: '永乐大典编纂',
    date: '1408-11-01',
    type: 'cultural',
    importance: 'major',
    description: '永乐帝命解缙等人编纂《永乐大典》',
    participants: ['zhu-2-4'],
    location: '北京',
    onChainHash: '0x6789...f012',
  },
  {
    id: 'event-7',
    title: '仁宣之治',
    date: '1424-09-07',
    type: 'governance',
    importance: 'major',
    description: '仁宗、宣宗相继治国，开创盛世',
    participants: ['zhu-3-2', 'zhu-4-1'],
    location: '北京',
    onChainHash: '0x789a...0123',
  },
];

/**
 * 转换为树形结构
 */
export function buildFamilyTree(members: ZhuMember[]): any {
  const memberMap = new Map(members.map((m) => [m.id, { ...m, children: [] }]));
  const root = memberMap.get('zhu-1');

  members.forEach((member) => {
    if (member.children) {
      const parent = memberMap.get(member.id);
      member.children.forEach((childId) => {
        const child = memberMap.get(childId);
        if (child && parent) {
          parent.children.push(child);
        }
      });
    }
  });

  return root;
}
